﻿#include "Input.h"
#include <QHBoxLayout>
#include <QDebug>
#include <QKeyEvent>

namespace GeneralUI {

	const QString defaultQss =
		"QWidget#Input QPushButton#leftButton"
		"{"
		"background-image:url(:/svg/Res/svg/arrow_left.svg); "
		"background-repeat:norepeat;"
		"background-position: center center;"
		"border:1px solid rgb(206, 206, 206);"
		"border-right:none;"
		"background-color: rgb(245, 245, 245);"
		"}"
		"QWidget#Input QPushButton#leftButton:hover"
		"{"
		"background-image:url(:/svg/Res/svg/arrow_left_hover.svg);"
		"background-color: rgb(36, 83, 164);"
		"}"
		"QWidget#Input QPushButton#leftButton:disabled"
		"{"
		"background-image:url(:/svg/Res/svg/arrow_left_disable.svg);"
		"background-color: rgb(241, 241, 241);"
		"}"
		"QWidget#Input QPushButton#rightButton"
		"{"
		"background-image:url(:/svg/Res/svg/arrow_right.svg);"
		"background-repeat:norepeat;"
		"background-position: center center;"
		"border:1px solid rgb(206, 206, 206);"
		"border-left:none;"
		"background-color: rgb(245, 245, 245);"
		"}"
		"QWidget#Input QPushButton#rightButton:hover"
		"{"
		"background-image:url(:/svg/Res/svg/arrow_right_hover.svg);"
		"background-color: rgb(36, 83, 164);"
		"}"
		"QWidget#Input QPushButton#rightButton:disabled"
		"{"
		"background-image:url(:/svg/Res/svg/arrow_right_disable.svg);"
		"background-color: rgb(241, 241, 241);"
		"}"
		"QWidget#Input QDoubleSpinBox#doubleSpinbox"
		"{"
		"color:#333;font-family:\"Arial\";"
		"font-size:14px;"
		"border:1px solid rgb(206, 206, 206);"
		"background-color:rgb(255, 255, 255);"
		"}"
		"QWidget#Input QDoubleSpinBox#doubleSpinbox:hover"
		"{"
		"border:1px solid rgb(36, 83, 164);"
		"}"
		"QWidget#Input QDoubleSpinBox#doubleSpinbox:disabled"
		"{"
		"color:rgb(187, 187, 187);"
		"background-color: rgb(241, 241, 241);"
		"}"
		"QWidget#Input QDoubleSpinBox#doubleSpinbox:focus"
		"{"
		"border:1px solid rgb(36, 83, 164);"
		"}";

	Input::Input(QWidget *parent)
		: UiWidgetBase(parent)
		, m_isShowButton(true)
		, m_leftButton(new QPushButton(this))
		, m_rightButton(new QPushButton(this))
		, m_spinBox(new SpinBox(this))
	{
		init();
	}

	Input::~Input()
	{

	}

	void Input::setSingleStep(int step)
	{
		m_spinBox->setSingleStep(step);
	}

	void Input::setValue(int val)
	{
		m_spinBox->setValue(val);
	}

	void Input::setPrefix(const QString &prefix)
	{
		m_spinBox->setPrefix(prefix);
	}

	void Input::setSuffix(const QString &suffix)
	{
		m_spinBox->setSuffix(suffix);
	}

	void Input::setRange(int minimum, int maximum)
	{
		m_spinBox->setRange(minimum, maximum);
	}

	void Input::setMaximum(int maximum)
	{
		m_spinBox->setMaximum(maximum);
	}

	void Input::setMinimum(int minimum)
	{
		m_spinBox->setMinimum(minimum);
	}

	void Input::setShowButton(bool show)
	{
		m_isShowButton = show;
		m_leftButton->setVisible(m_isShowButton);
		m_rightButton->setVisible(m_isShowButton);
		update();
	}

	void Input::setScreenMapping(bool mapping)
	{
		m_spinBox->setScreenMapping(mapping);
	}

	void Input::setAlignment(Qt::Alignment flag)
	{
		m_spinBox->setAlignment(flag);
	}

	int Input::singleStep() const
	{
		return m_spinBox->singleStep();
	}

	int Input::value() const
	{
		return m_spinBox->value();
	}

	QString Input::prefix() const
	{
		return m_spinBox->prefix();
	}

	QString Input::suffix() const
	{
		return m_spinBox->suffix();
	}

	int Input::maximum() const
	{
		return m_spinBox->maximum();
	}

	int Input::minimum() const
	{
		return m_spinBox->minimum();
	}

	bool Input::isShowButton()
	{
		return m_isShowButton;
	}

	bool Input::isScreenMapping()
	{
		return m_spinBox->isScreenMapping();
	}

	Qt::Alignment Input::getAlignment()
	{
		return m_spinBox->alignment();
	}

	bool Input::hasFocus()
	{
		bool focus = m_spinBox->hasFocus();
		focus |= m_leftButton->hasFocus();
		focus |= m_rightButton->hasFocus();
		return focus;
	}

	bool Input::blockSignals(bool block)
	{
		bool result = m_spinBox->blockSignals(block);
		result &= m_leftButton->blockSignals(block);
		result &= m_rightButton->blockSignals(block);
		return result;
	}

	void Input::setObjectName(const QString & name)
	{
		Q_UNUSED(name);

		__super::setObjectName(QString::fromUtf8("Input"));
	}

	void Input::slotLeftButtonClicked()
	{
		int curValue = m_spinBox->value();
		curValue -= m_spinBox->singleStep();
		curValue = qMax(curValue, m_spinBox->minimum());
		m_spinBox->setValue(curValue);
		emit signalEditingFinished();
	}

	void Input::slotRightButtonClicked()
	{
		int curValue = m_spinBox->value();
		curValue += m_spinBox->singleStep();
		curValue = qMin(curValue, m_spinBox->maximum());
		m_spinBox->setValue(curValue);
		emit signalEditingFinished();
	}

	void Input::paintEvent(QPaintEvent *event)
	{
		m_spinBox->setFixedHeight(this->height());
		m_leftButton->setFixedSize(this->height(), this->height());
		m_rightButton->setFixedSize(this->height(), this->height());
		return QWidget::paintEvent(event);
	}

	void Input::init()
	{
		QHBoxLayout *horLayout = new QHBoxLayout(this);

		horLayout->addWidget(m_leftButton);
		horLayout->addWidget(m_spinBox);
		horLayout->addWidget(m_rightButton);
		horLayout->setSpacing(0);
		horLayout->setContentsMargins(0, 0, 0, 0);
		this->setLayout(horLayout);
		m_spinBox->setButtonSymbols(QAbstractSpinBox::NoButtons);

		m_leftButton->setAutoDefault(false);
		m_rightButton->setAutoDefault(false);

		this->setObjectName(QString::fromUtf8("Input"));
		m_leftButton->setObjectName(QString::fromUtf8("leftButton"));
		m_rightButton->setObjectName(QString::fromUtf8("rightButton"));

		connect(m_leftButton, &QPushButton::clicked, this, &Input::slotLeftButtonClicked);
		connect(m_rightButton, &QPushButton::clicked, this, &Input::slotRightButtonClicked);
		connect(m_spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &Input::signalValueChanged);
		connect(m_spinBox, &QDoubleSpinBox::editingFinished, this, &Input::signalEditingFinished);

		this->show();
	}

}